package com.dreamwhale.machinery.goods.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.config.DataResult;
import com.dreamwhale.machinery.entity.YxStoreProduct;
import com.dreamwhale.machinery.goods.entity.YxStoreCart;
import com.dreamwhale.machinery.goods.mapper.YxStoreProductMapper;
import com.dreamwhale.machinery.goods.service.YxStoreCartService;
import com.dreamwhale.machinery.user.entity.UserShop;
import com.dreamwhale.machinery.user.mapper.UserShopMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author Author
 * @since 2020-05-28
 */
@RestController
@RequestMapping("/yxStoreCart")
public class YxStoreCartController {

    @Autowired
    private YxStoreCartService yxStoreCartService;

    @Resource
    private YxStoreProductMapper yxStoreProductMapper;

    @Resource
    private UserShopMapper userShopMapper;


    /**
     * hao
     * 购物车信息
     *
     * @param userId
     * @return
     */
    @RequestMapping("/getStoreCart")
    /**
     * @MethodName : getStoreCart
     * @Description : 获取购物车
     * @param : userId
     * @Author : DragonUncle
     * @Data : 10:58 2020/6/30
     * @return :
     */
    public DataResult getStoreCart(Integer userId) {
        List<YxStoreCart> list = yxStoreCartService.getStoreCart(userId);
        if (list != null) {
            for (YxStoreCart YxStoreCart : list) {
                //商品信息
                QueryWrapper<YxStoreProduct> yStoreProduct = new QueryWrapper<>();
                yStoreProduct.eq("id", list.get(0).getProductId());
                List<YxStoreProduct> listp = yxStoreProductMapper.selectList(yStoreProduct);
                //单价
                YxStoreCart.setPrice(listp.get(0).getPrice());
                //总价
                BigDecimal prices = BigDecimal.valueOf(YxStoreCart.getCartNum());
                YxStoreCart.setTotalPrice(listp.get(0).getPrice().multiply(prices));
                //商品名称
                YxStoreCart.setStoreName(listp.get(0).getStoreName());
                //商品图片
                YxStoreCart.setImage(listp.get(0).getImage());
                //邮费
                YxStoreCart.setPostage(listp.get(0).getPostage());
                //店铺信息
                QueryWrapper<UserShop> userShop = new QueryWrapper<>();
                yStoreProduct.eq("id", listp.get(0).getMerId());
                List<UserShop> listu = userShopMapper.selectList(userShop);
                //店铺名称
                YxStoreCart.setMerchantName(listu.get(0).getMerchantName());
            }

        }
        return DataResult.ok(list);
    }

    @RequestMapping("/addStoreCart")
    /**
     * @MethodName : addStoreCart
     * @Description : 添加购物车
     * @param : yxStoreCart
     * @param : request
     * @Author : DragonUncle
     * @Data : 10:58 2020/6/30
     * @return :
     */
    public ResponseEntity<DataResult> addStoreCart(YxStoreCart yxStoreCart, HttpServletRequest request) {
        //获取token中的userId
        //String userId = request.getParameter("userId");
        return ResponseEntity.ok(yxStoreCartService.addStoreCart(yxStoreCart));
    }

    @RequestMapping("/changeStoreCart")
    /**
     * @MethodName : changeStoreCart
     * @Description : 修改购物车
     * @param : yxStoreCart
     * @Author : DragonUncle
     * @Data : 10:58 2020/6/30
     * @return :
     */
    public ResponseEntity<DataResult> changeStoreCart(YxStoreCart yxStoreCart) {
        //获取token中的userId
        //String userId = request.getParameter("userId");
        return ResponseEntity.ok(yxStoreCartService.changeStoreCart(yxStoreCart));
    }

    @RequestMapping("/delStoreCart")
    /**
     * @MethodName : delStoreCart
     * @Description : 删除购物车
     * @param : yxStoreCart
     * @Author : DragonUncle
     * @Data : 10:57 2020/6/30
     * @return :
     */
    public ResponseEntity<DataResult> delStoreCart(YxStoreCart yxStoreCart) {
        //获取token中的userId
        //String userId = request.getParameter("userId");
        return ResponseEntity.ok(yxStoreCartService.delStoreCart(yxStoreCart));
    }

    /**
     * 全选删除
     *
     * @param
     * @return
     */
    @RequestMapping("/delStoreCartAll")
    public DataResult delStoreCartAll(YxStoreCart yxStoreCart) {

        return DataResult.ok(yxStoreCartService.updateYxStoreCarts(yxStoreCart));
    }

}